package com.chance.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chance.common.exception.LeaseException;
import com.chance.common.result.ResultCodeEnum;
import com.chance.model.entity.*;
import com.chance.model.enums.ItemType;
import com.chance.web.admin.mapper.*;
import com.chance.web.admin.service.*;
import com.chance.web.admin.vo.apartment.ApartmentDetailVo;
import com.chance.web.admin.vo.apartment.ApartmentItemVo;
import com.chance.web.admin.vo.apartment.ApartmentQueryVo;
import com.chance.web.admin.vo.apartment.ApartmentSubmitVo;
import com.chance.web.admin.vo.fee.FeeValueVo;
import com.chance.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author HUANG
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2024-10-30 12:33:34
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentLabelMapper labelMapper;

    @Autowired
    private ApartmentFacilityMapper facilityMapper;

    @Autowired
    private ApartmentFeeValueMapper feeValueMapper;

    @Autowired
    private ApartmentInfoMapper infoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;


    /**
     * @param apartmentSubmitVo 保存或修改的数据
     */
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        // 判断是否更新操作
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        if (isUpdate) {
            Long id = apartmentSubmitVo.getId();
            // 删除关联信息
            removeApartmentInfoById(id);
        }
        super.saveOrUpdate(apartmentSubmitVo);
        // 添加图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            graphInfoService.saveBathById(graphVoList, ItemType.APARTMENT, apartmentSubmitVo.getId());
        }
        // 添加配套列表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            facilityInfoIds.forEach(facilityInfoId -> {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityInfoId);
                apartmentFacilities.add(apartmentFacility);
            });
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }
        // 插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            labelIds.forEach(labelId -> {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                apartmentLabels.add(apartmentLabel);
            });
            apartmentLabelService.saveBatch(apartmentLabels);
        }
        // 插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            feeValueIds.forEach(feeValueId -> {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValues.add(apartmentFeeValue);
            });
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }
    }


    /**
     * @param page 分页信息
     * @param queryVo 查询字段
     * @return 响应数据
     */
    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return infoMapper.pageApartmentItemByQuery(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        // 查询公寓信息
        ApartmentInfo detailById = this.getById(id);
        // 查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectItemTypeAndId(ItemType.APARTMENT,id);
        // 查询标签列表
        List<LabelInfo> labelInfoList  = labelMapper.selectListByApartmentId(id);
        // 查询配套列表
        List<FacilityInfo> facilityInfos  = facilityMapper.selectListByApartmentId(id);
        // 查询杂费列表
        List<FeeValueVo> feeValueVos = feeValueMapper.selectListByApartmentId(id);

        BeanUtils.copyProperties(detailById, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);
        return apartmentDetailVo;
    }

    @Override
    public void removeId(Long id) {
        // 判断公寓下是否有房间信息
        Long l = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if (l > 0) {
            // 有房间信息就不能删除公寓返回失败信息
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        // 删除公寓信息
        super.removeById(id);
        // 删除关联信息
        removeApartmentInfoById(id);
    }

    private void removeApartmentInfoById(Long id) {
        // 删除关联图片信息
        graphInfoService.remove(new LambdaUpdateWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId, id));
        // 删除配套信息   
        apartmentFacilityService.remove(new LambdaUpdateWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, id));
        // 删除标签信息
        apartmentLabelService.remove(new LambdaUpdateWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, id));
        // 删除杂费信息、
        apartmentFeeValueService.remove(new LambdaUpdateWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, id));
    }
}




